import cv2
import math
import os


def generate_video_by_image(output_path, dir_num, root_path, image_names):
    """

        :param dir_num:
        :param output_path:
        :param root_path: 输入image_names 的目录
        :param image_names: 输入image的名称  type:str
        :return:
        """
    fps = 30
    # 图片尺寸
    img_size = (640, 480)
    fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
    number = len(image_names)
    if number >= 180:
        i = 0
        frames = []
        for img_name in image_names:
            img = cv2.imread(os.path.join(root_path, img_name))
            img = cv2.resize(img, img_size)
            frames.append(img)
        length = math.floor((number / 30))
        for i in range(0, length):
            name = str(dir_num) + '-FemaleNoGlasses_' + str(i) + '-Talking.avi'
            path = os.path.join(output_path, name)
            face_video_writer = cv2.VideoWriter(path, fourcc, fps, img_size)
            for im_f in frames[i * 30:i * 30 + 180]:
                face_video_writer.write(im_f)
            face_video_writer.release()
            print("video named '{}' has generated".format(path))
    print('finish')
    pass
